require(["config"], function(config) {
	require(["jquery", "sw", "cook", "cooki", "one", ], function(jquery, sw, cook, cooki, one) {

		$(function() {
			//左边的树形图
			$(".tubiao ul").hide();
			$(".tubiao:has(ul)").css("list-style-image", "url(img/left_category_add.jpg)");
			$(".tubiao:has(ul)").click(function(event) { //为包含ul元素的li定义点击事件
				if (event.target == this) {
					if ($(this).children().is(":hidden")) {
						//console.log( $(this).children() );
						$(this).css("list-style-image", "url(img/left_category_low.jpg)");
						//console.log("1")
					} else {
						$(this).css("list-style-image", "url(img/left_category_add.jpg)");
						//console.log("2")
					}
					$(this).children().toggle(500);
				}
			});
			$(".tubiao:not(:has(ul))").css("list-style-position", "inside");

		})
		$(function() {
			//展开收起列表
			$(".button_border").click(function() {
				//console.log("hahah")
				var state = $(this).attr("state");
				if (state == "close") {
					$(".none").show();
					// $(this).addClass("button_border_2")
					$(this).text('收起高级搜索');
					$(".button_border").attr("state", "open");
					// console.log($(this))
				} else if (state == "open") {
					$(".none").hide();
					// $(this).removeClass("button_border_2")
					$(this).text('展开高级搜索');
					$(".button_border").attr("state", "close");
					// $(this).parent().show();
				}
			})

			//点击显示已选项
			//初始化点击选项以及点击后的容器
			$("#chose").css("display", "none");
			$("#sea dd a:first-child").css("color", "#ff8a81").siblings().css("color", "#9898A4");

			$("#sea dd").find("a").click(function() {

					$("<a href='#javascript' class='chosea'>" + $(this).text() + " <i>×</i></a>").appendTo($("#chose dd"));
					//$("#sea").css("overflow","hidden")
					//添加选项后chose的框显示出来

					//console.log($("#chose dd a").length)
					//初始化选择容器
					$("#chose").css("display", "block");
					//点击×关闭选项
					$(".chosea i").click(function() {
						if (($("#chose dd a").length) == 0) {

							$("#chose").css("display", "none");
						} else {
							$(this).parent().remove();

						}
					})

					$(this).css("color", "#ff8a81").siblings().css("color", "#9898A4");

				})
				//点击清空按钮	
			$(".chose_clear").click(function() {
				$("#chose dd a").remove();
				$("#chose").css("display", "none")
			})

			//给four1点击换图片
			$(".four1 ul li").click(function() {
				$(this).css({
					"background": "#fff",
					"color": "#ff8a81"
				}).find("img").attr("src", "img/list_right_xia.png")
				$(this).siblings().css({
					"background": "#efefef",
					"color": "#666"
				}).find("img").attr("src", "img/list_right_shang.png");
				//console.log($(this).siblings())
			})

			$.each($(".five1 ul li"), function(index) {
				$(this).bind({
					mouseover: function() {
						$(this).find(".kuang").css("opacity", 1)

						var xia = $(this).index() + 1;
						$(this).find("img").attr("src", "img/jg" + xia + ".jpg");
						//console.log(i)
						//console.log(xia)
						//				$("img").attr("src","test.jpg");
					},
					mouseout: function() {
						$(this).find(".kuang").css("opacity", 0);

						var hou = $(this).index() + 1
						$(this).find("img").attr("src", "img/gj" + hou + ".jpg");
						//console.log(hou)

					}

				})
			})
			$(".five1 ul li").click(function() {
				location.href = "detail.html";
			})

			$.getJSON("js/list.json", function(json) {
				var li = $(".five1 ul li")
					//		alert(json[1].img)	
					//		alert(json[1].name)
				$.each(json, function(i) {
					$("<img src=" + json[i].img + " data-good-id=" + [i] + ">").appendTo(li.eq(i).children("a"))

					$("<p>" + json[i].name + "</p>").appendTo(li.eq(i).children("a"))
					$("<span>" + json[i].price + "</span>").appendTo(li.eq(i).children("a"))
					$("<p>" + json[i].pingjia + "</p>").appendTo(li.eq(i).children("a"))
					$("<span class='kuang'>" + json[i].span + "</span>").appendTo(li.eq(i).children("a"))
				})
			})
			$(".top_right_car").click(function() {
				location.href = "shopping.html"
			})

		})

		$(function() {
			function loadCart() {
				var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
				var cartObj = convertCartStrToObj(cartStr);
				//获取到购物车中所有商品的数量
				var total = 0;
				for (var id in cartObj) {
					total += cartObj[id].num;
				}
				$(".top_right_span1").html(total);
				$(".top_right_span1").val(total);
			}
			loadCart()
		})

		function convertCartStrToObj(cartStr) {
			if (!cartStr) {
				return {};
			}
			var goods = cartStr.split(":");
			var obj = {};
			for (var i = 0; i < goods.length; i++) {
				var data = goods[i].split(",");
				//以商品的id为健 商品的其他信息为值
				obj[data[0]] = {
					name: data[1],
					price: parseFloat(data[2]),
					num: parseInt(data[3]),
					src: data[4]
				}
			}
			return obj;
		}

		function convertObjToCartStr(obj) {
			var cartStr = "";
			//遍历对象
			for (var id in obj) {
				if (cartStr) {
					cartStr += ":"
				}
				cartStr += id + "," + obj[id].name + "," + obj[id].price + "," + obj[id].num + "," + obj[id].src + ",";

			}
			return cartStr
		}

	})
})